package ua.khpi.si.kit49.diploma.db.dto;

import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.Table;

import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;

/**
 * Class represents users in this system
 * 
 * @author netstalk3r
 * 
 */

@Entity
@Table(name = "users")
public class User {

	@Id
	@GeneratedValue
	@Column(name = "id", unique = true, nullable = false)
	private Integer id;

	@Column(name = "username", unique = true, length = 45)
	private String username;

	@Column(name = "password", unique = true, length = 45)
	private String password;

	@Column(name = "email", unique = true, length = 45)
	private String email;

	@Basic
	@Column(name = "enabled", columnDefinition = "BIT", length = 1)
	private Boolean enabled;

	@OneToOne(mappedBy = "user", cascade = CascadeType.ALL)
	@Fetch(FetchMode.JOIN)
	UserRole userRole;

	public User() {
		super();
	}

	public User(String username, String password, String email,
			Boolean enabled, UserRole userRole) {
		super();
		this.username = username;
		this.password = password;
		this.email = email;
		this.enabled = enabled;
		this.userRole = userRole;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public Boolean getEnabled() {
		return enabled;
	}

	public void setEnabled(Boolean enabled) {
		this.enabled = enabled;
	}

	public UserRole getUserRole() {
		return userRole;
	}

	public void setUserRole(UserRole userRole) {
		this.userRole = userRole;
	}

	@Override
	public String toString() {
		return "User: id - " + id + "; username - " + username + "; email - "
				+ email;
	}

	@Override
	public int hashCode() {
		int hash = 43;
		hash = hash * 17 + ((id == null) ? 0 : id.hashCode());
		hash = hash * 17 + ((username == null) ? 0 : username.hashCode());
		hash = hash * 17 + ((password == null) ? 0 : password.hashCode());
		hash = hash * 17 + ((email == null) ? 0 : email.hashCode());
		hash = hash * 17 + ((enabled == null) ? 0 : enabled.hashCode());
		return hash;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (!(obj instanceof User))
			return false;
		User entry = (User) obj;
		return id.equals(entry.getId()) && username.equals(entry.getUsername())
				&& password.equals(entry.getPassword())
				&& email.equals(entry.getEmail())
				&& enabled.equals(entry.getEnabled());
	}
}
